package com.zhuzhu.task;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.zhuzhu.util.HttpGetter;
import com.zhuzhu.util.ZhuzhuProperty;

@Component
public class DailyFetchJob {
	
	private static final Logger logger = LoggerFactory.getLogger(DailyFetchJob.class);
	
	private final String 		FILE_NAME_PREFIX 	= "ZZ_NASDAQ_";			//文件名前缀
	private final HttpGetter 	HTTP_GETTER 		= new HttpGetter(); 	//Http Get方法请求
	private final String 		NASDAQ_URL_PREFIX	= "http://hq.sinajs.cn/etag.php?_=";
	private final String 		NASDAQ_URL_PARAM	= "&list=gb_$inx,gb_ixic";

	private FileWriter 	writer;			//写文件工具
	private DateTime 	now;			//当前时间
	private String 		lastContent;	//上一条内容
	
	private String 		nasdaqNum;		//纳斯达克指数
	private String 		nasdaqTime;		//纳斯达克时间
	private String 		standPorNum;	//标普指数
	private String 		standPorTime;	//标普时间
	
	

	/**
	 * 执行任务
	 * @author liubo
	 * @version 2016年8月10日
	 */
	public void execute(){
		now = new DateTime();
		
		try {
			resolveInfo(HTTP_GETTER.get(getUrl())); 
			writeFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 解析信息
	 * @author liubo
	 * @version 2016年8月10日
	 * @param ixic
	 */
	public void resolveInfo(String ixic){
		
		String[] strings = ixic.split(";");
		String standPor = strings[0];
		String nasdaq = strings[1];
		
		resolveStandardPoor(standPor);
		resolveNasdaq(nasdaq);
	}
	
	public void resolveStandardPoor(String standardPoor){
		standPorNum = standardPoor.split(",")[1];
		standPorTime = standardPoor.split(",")[3];
	}
	
	public void resolveNasdaq(String nasdaq){
		nasdaqNum = nasdaq.split(",")[1];
		nasdaqTime = nasdaq.split(",")[3];
	}
	
	/**
	 * 创建sina 纳斯达克接口 url
	 * @author liubo
	 * @version 2016年8月10日
	 * @return
	 */
	public String getUrl(){
		return NASDAQ_URL_PREFIX+now.toDate().getTime()+NASDAQ_URL_PARAM;
	}
	
	//每次创建FileWriter成本较高，需要优化
	public void writeFile() throws IOException{
		String content = nasdaqNum+","+nasdaqTime+","+standPorNum+","+standPorTime;
		if (!content.equals(lastContent)) {
			initFileWriter();
			writer.write(content+"\n");
			writer.flush();
			logger.info(content);
		}else { //如果数据相同，表示数据没有更新，股市停盘，可以结束当日的写文件工作
			destroyFileWriter();
		}
		lastContent = content;
	}
	
	/**
	 * 创建FileWriter
	 * @author liubo
	 * @version 2016年8月10日
	 * @return
	 * @throws IOException
	 */
	public void initFileWriter() throws IOException{
		if (writer == null) {
			writer = new FileWriter(new File(getFileFullName()),true); 
		}
	}
	
	/**
	 * 关闭FileWriter
	 * @author liubo
	 * @version 2016年10月18日
	 * @throws IOException
	 */
	public void destroyFileWriter() throws IOException{
		if (writer != null) {
			this.writer.flush();
			this.writer.close();
			this.writer = null;
			logger.info("distroy file writer.");
		}
	}
	
	/**
	 * 创建文件全路径名
	 * @author liubo
	 * @version 2016年8月10日
	 * @return
	 */
	public String getFileFullName(){
		String date = null;
		if (isPM()) {//如果是下午，使用当天时间
			date = now.toString("yyyy_MM_dd");
		}else {//如果是上午，使用前一天日期做文件名
			DateTime lastDay = now.minusDays(1);
			date = lastDay.toString("yyyy_MM_dd");
		}
		
		return ZhuzhuProperty.FILE_PATH+FILE_NAME_PREFIX+date+".csv";
	}
	
	/**
	 * 判断时间是否是下午
	 * @author liubo
	 * @version 2016年8月10日
	 * @return
	 */
	public boolean isPM(){
		return now.getHourOfDay()> 12;
	}
	
	public static void main(String[] args) {
		DailyFetchJob job = new DailyFetchJob();
		job.execute();
	}

}
